//
//  TileView.m
//  etionUI
//
//  Created by wangjian on 7/17/13.
//
//

#import "TileView.h"

#define ICONSIZE    50
#define TITLEFONTH  16
#define INFOFONTH   10
#define TV_MARGIN      10

#define DURATION    0.5

@implementation CTileUpView

@synthesize iconView = _iconView;

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {

    }
    return self;
}

- (void)dealloc
{
    [_iconView release];
    [super dealloc];
}

- (UIImageView *)iconView
{
    if (_iconView == nil)
    {
        _iconView = [[UIImageView alloc] initWithFrame:CGRectMake((self.width-ICONSIZE)/2, (self.height-ICONSIZE)/2, ICONSIZE, ICONSIZE)];
        [self addSubview:_iconView];
    }
    return _iconView;
}

- (void)moveIconUpAnimated:(BOOL)animated
{
    if (animated)
    {
        [UIView animateWithDuration:DURATION animations:^{
            self.iconView.frame = CGRectMake((self.width-ICONSIZE)/2, (self.height-ICONSIZE-TITLEFONTH)/2, ICONSIZE, ICONSIZE);
        } completion:^(BOOL finished) {
            
        }];
    }
    else
    {
        self.iconView.frame = CGRectMake((self.width-ICONSIZE)/2, (self.height-ICONSIZE-TITLEFONTH)/2, ICONSIZE, ICONSIZE);
    }
}

@end

//===============================================================
//      CTileDownView

@interface CTileDownView()

@end

@implementation CTileDownView

@synthesize titleLabel1 = _titleLabel1;
@synthesize titleLabel2 = _titleLabel2;
@synthesize valueLabel1 = _valueLabel1;
@synthesize valueLabel2 = _valueLabel2;
@synthesize style = _style;

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        self.backgroundColor = [UIColor whiteColor];
        
        _titleLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(TV_MARGIN/2, (self.height-INFOFONTH)/2, 0, INFOFONTH)];
        _titleLabel1.font = [UIFont systemFontOfSize:INFOFONTH];
        _titleLabel1.textColor = [UIColor colorWithHex:0xa2a7ad alpha:1.0];
        _titleLabel1.backgroundColor = [UIColor clearColor];
        [self addSubview:_titleLabel1];
        
        _titleLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(self.width/2 + TV_MARGIN/2, (self.height-INFOFONTH)/2, 0, INFOFONTH)];
        _titleLabel2.font = [UIFont systemFontOfSize:INFOFONTH];
        _titleLabel2.textColor = [UIColor colorWithHex:0xa2a7ad alpha:1.0];
        _titleLabel2.backgroundColor = [UIColor clearColor];
        [self addSubview:_titleLabel2];
        
        _valueLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(TV_MARGIN/2, (self.height-INFOFONTH)/2, 0, INFOFONTH)];
        _valueLabel1.font = [UIFont systemFontOfSize:INFOFONTH];
        _valueLabel1.backgroundColor = [UIColor clearColor];
        [self addSubview:_valueLabel1];
        
        _valueLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(self.width/2 + TV_MARGIN/2, (self.height-INFOFONTH)/2, 0, INFOFONTH)];
        _valueLabel2.font = [UIFont systemFontOfSize:INFOFONTH];
        _valueLabel2.backgroundColor = [UIColor clearColor];
        [self addSubview:_valueLabel2];
        
        self.style = ETileDownViewStyleOneRow;
    }
    return self;
}

- (void)dealloc
{
    [_titleLabel1 release];
    [_titleLabel2 release];
    [_valueLabel1 release];
    [_valueLabel2 release];
    [super dealloc];
}

- (void)setTitle1:(NSString *)title1 value1:(NSString *)value1 title2:(NSString *)title2 value2:(NSString *)value2 animated:(BOOL)animated
{
    CGSize title1Size = [title1 stringSizeWithFont:self.titleLabel1.font];
    CGSize value1Size = [value1 stringSizeWithFont:self.valueLabel1.font];
    CGSize title2Size = [title2 stringSizeWithFont:self.titleLabel2.font];
    CGSize value2Size = [value2 stringSizeWithFont:self.valueLabel2.font];
    
    ETileDownViewStyle newStyle = ETileDownViewStyleOneRow;
    if (title1Size.width+value1Size.width < (self.width-5*TV_MARGIN/2)/2 && title2Size.width+value2Size.width < (self.width-5*TV_MARGIN/2)/2)
    {
        newStyle = ETileDownViewStyleOneRow;
    }
    else
    {
        newStyle = ETileDownViewStyleTowRow;
    }
    
    CGRect title1NewFrame;
    CGRect value1NewFrame;
    CGRect title2NewFrame;
    CGRect value2NewFrame;

    self.style = newStyle;
    if (newStyle == ETileDownViewStyleOneRow)
    {
        title1NewFrame = CGRectMake(TV_MARGIN/2, (self.height-title1Size.height)/2, title1Size.width, title1Size.height);
        value1NewFrame = CGRectMake(CGRectGetMaxX(title1NewFrame) + TV_MARGIN/2, (self.height-value1Size.height)/2, self.width/2-1.25*TV_MARGIN-title1Size.width, value1Size.height);
        title2NewFrame = CGRectMake(CGRectGetMaxX(value1NewFrame) + TV_MARGIN/2, (self.height-title2Size.height)/2, title2Size.width, title2Size.height);
        value2NewFrame = CGRectMake(CGRectGetMaxX(title2NewFrame) + TV_MARGIN/2, (self.height-value2Size.height)/2, self.width/2-1.25*TV_MARGIN-title2Size.width, value2Size.height);
    }
    else
    {
        title1NewFrame = CGRectMake(TV_MARGIN/2, (self.height-title1Size.height-value1Size.height)/2, self.width/2 - 0.75*TV_MARGIN, title1Size.height);
        value1NewFrame = CGRectMake(TV_MARGIN/2, CGRectGetMaxY(title1NewFrame), CGRectGetWidth(title1NewFrame), value1Size.height);
        title2NewFrame = CGRectMake(CGRectGetMaxX(title1NewFrame) + TV_MARGIN/2, CGRectGetMinY(title1NewFrame), CGRectGetWidth(title1NewFrame), title2Size.height);
        value2NewFrame = CGRectMake(CGRectGetMaxX(title1NewFrame) + TV_MARGIN/2, CGRectGetMaxY(title1NewFrame), CGRectGetWidth(title1NewFrame), value2Size.height);
    }

    if (animated == NO)
    {
        self.titleLabel1.frame = title1NewFrame;
        self.valueLabel1.frame = value1NewFrame;
        self.titleLabel2.frame = title2NewFrame;
        self.valueLabel2.frame = value2NewFrame;
        
        self.titleLabel1.text = title1;
        self.valueLabel1.text = value1;
        self.titleLabel2.text = title2;
        self.valueLabel2.text = value2;
    }
    else
    {
        [UIView animateWithDuration:.25 animations:^{
            self.titleLabel1.frame = title1NewFrame;
            self.titleLabel2.frame = title2NewFrame;
            self.titleLabel1.text = title1;
            self.titleLabel2.text = title2;
        }];
        [self setValue:value1 forValueLabel:self.valueLabel1 newFrame:value1NewFrame animated:YES];
        [self setValue:value2 forValueLabel:self.valueLabel2 newFrame:value2NewFrame animated:YES];
    }
}

- (void)setValue:(NSString *)value forValueLabel:(UILabel *)valueLable newFrame:(CGRect)newFrame animated:(BOOL)animated
{
    if (animated == NO || valueLable.text.length == 0)
    {
        valueLable.text = value;
        valueLable.frame = newFrame;
    }
    else
    {
        UILabel *oldValueLable = [[UILabel alloc] initWithFrame:valueLable.frame];
        oldValueLable.font = valueLable.font;
        oldValueLable.textColor = valueLable.textColor;
        oldValueLable.text = valueLable.text;
        oldValueLable.backgroundColor = [UIColor clearColor];
        [self addSubview:oldValueLable];

        valueLable.alpha = 0.0;
        valueLable.text = value;
        
        [UIView animateWithDuration:.25 animations:^{
            valueLable.frame = newFrame;
            valueLable.alpha = 1.0;
            
            oldValueLable.frame = newFrame;
            oldValueLable.alpha = 0.0;
        } completion:^(BOOL finished) {
            [oldValueLable removeFromSuperview];
            [oldValueLable release];
        }];
    }
}

//- (void)setValue1:(NSString *)value animated:(BOOL)animated
//{
//    [self setValue:value forValueLabel:self.valueLabel1 animated:animated];
//}
//
//- (void)setValue2:(NSString *)value animated:(BOOL)animated
//{
//    [self setValue:value forValueLabel:self.valueLabel2 animated:animated];
//}

@end


//===============================================================
//      CTileBadgeView

#define PARENT_MARGIN_W 35
#define PARENT_MARGIN_H 20

@interface CTileBadgeView()

@property (nonatomic, assign) UILabel *textLabel;

@end

@implementation CTileBadgeView

+ (CTileBadgeView *)tileBadgeViewForParentView:(UIView *)parentView
{
    CTileBadgeView *badgeView = [[[CTileBadgeView alloc] initWithFrame:CGRectZero] autorelease_mm];
    badgeView.parentView = parentView;
    return badgeView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.layer.masksToBounds = YES;
        self.backgroundColor = [UIColor whiteColor];
        self.layer.masksToBounds = YES;
        
        self.textLabel = [[UILabel alloc] initWithFrame:self.bounds];
        self.textLabel.textAlignment = NSTextAlignmentCenter;
        self.textLabel.textColor = [UIColor whiteColor];
        self.textLabel.backgroundColor = [UIColor colorWithHex:0xE74C3C];
        self.textLabel.font = [UIFont systemFontOfSize:12];
        self.textLabel.layer.masksToBounds = YES;
        [self addSubview:self.textLabel];
    }
    return self;
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    self.layer.cornerRadius = frame.size.height/2;
}

- (void)setTextColor:(UIColor *)textColor
{
    self.textLabel.textColor = textColor;
}

- (void)setParentView:(UIView *)parentView
{
    _parentView = parentView;
    self.frame = CGRectMake(self.parentView.width-self.textLabel.width-PARENT_MARGIN_W, PARENT_MARGIN_H, self.textLabel.width, self.textLabel.height);
}

- (void)setBadgeText:(NSString *)badgeText
{
    const CGFloat margin = 2;
    const CGFloat standH = 24;
    const CGFloat maxW = 36;
    const CGFloat minW = standH-2*margin;
    
    if (badgeText.length == 0)
    {
        if (_badgeText.length > 0)  //隐藏
        {
            self.textLabel.alpha = 0.0;
            self.frame = CGRectMake(self.parentView.width-PARENT_MARGIN_W, PARENT_MARGIN_H, 0, 0);
        }
    }
    else
    {
        if (self.superview == nil)  //显示
        {
            [self.parentView addSubview:self];
        }
        
        CGSize size = [badgeText stringSizeWithFont:self.textLabel.font];
        if (size.width > maxW)
        {
            size.width = maxW;
        }
        if (size.width < minW)
        {
            size.width = minW;
        }
        self.frame = CGRectMake(self.parentView.width-2*margin-size.width-PARENT_MARGIN_W, PARENT_MARGIN_H, size.width+2*margin, standH);
        self.textLabel.alpha = 1.0;
        self.textLabel.frame = CGRectInset(self.bounds, margin, margin);
        self.textLabel.layer.cornerRadius = self.textLabel.height/2;
    }
    
    self.textLabel.text = badgeText;
    
    NSString *tmp = [badgeText retain];
    [_badgeText release_mm];
    _badgeText = tmp;
}

- (void)setBadgeText:(NSString *)badgeText animated:(BOOL)animated
{
    if (animated == NO)
    {
        self.badgeText = badgeText;
    }
    else
    {
        [UIView animateWithDuration:.25 animations:^{
            self.badgeText = badgeText;
        }];
    }
}

@end


//===============================================================

#define DOWNVIEWH   34

@interface CTileView()

@property (nonatomic, retain) UIColor *themeColor;
@property (nonatomic, retain) CTileUpView *upView;
@property (nonatomic, retain) CTileDownView *downView;
@property (nonatomic, retain) UILabel *titleLabel;

@property (nonatomic, retain) UIView *downBgView;
@property (nonatomic, retain) UIView *emptyDownView;

@property (nonatomic, retain) CTileBadgeView *badgeView;

@end

@implementation CTileView

@synthesize themeColor = _themeColor;

@synthesize upView = _upView;
@synthesize downView = _downView;
@synthesize emptyDownView = _emptyDownView;
@synthesize titleLabel = _titleLabel;
@synthesize downBgView = _downBgView;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        
        _downBgView = [[UIView alloc] initWithFrame:CGRectMake(0, self.height-DOWNVIEWH, self.width, DOWNVIEWH)];
        _downBgView.backgroundColor = [UIColor clearColor];
        [self addSubview:_downBgView];
        
        _emptyDownView = [[UIView alloc] initWithFrame:self.downBgView.bounds];
        _emptyDownView.backgroundColor = [UIColor whiteColor];
        [self.downBgView addSubview:_emptyDownView];
        
        self.layer.shadowColor = [UIColor colorWithHex:0xcacdd1 alpha:1.0].CGColor;
        self.layer.shadowOffset = CGSizeMake(0, 1.5);
        self.layer.shadowOpacity = .5;
        self.layer.shadowRadius = 0.0;
    }
    return self;
}

- (CTileUpView *)upView
{
    if (_upView == nil)
    {
        _upView = [[CTileUpView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height-DOWNVIEWH)];
        [self addSubview:_upView];
    }
    return _upView;
}

- (UILabel *)titleLabel
{
    if (_titleLabel == nil)
    {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.height-DOWNVIEWH+(DOWNVIEWH-TITLEFONTH)/2, self.width, TITLEFONTH)];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.textColor = [UIColor colorWithHex:0x72767d];
        _titleLabel.font = [UIFont boldSystemFontOfSize:TITLEFONTH];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_titleLabel];
    }
    
    return _titleLabel;
}

- (void)setThemeColor:(UIColor *)themeColor
{
    UIColor *tmpColor = [themeColor retain];
    [_themeColor release];
    _themeColor = tmpColor;
    
    if (_upView != nil)
    {
        _upView.backgroundColor = themeColor;
    }
}

- (void)setBadgeText:(NSString *)badgeText
{
    _badgeText = badgeText;
    
    if (self.badgeView == nil && badgeText.length > 0)
    {
        self.badgeView = [CTileBadgeView tileBadgeViewForParentView:self];
//        self.badgeView.textColor = self.themeColor;
    }
    [self.badgeView setBadgeText:badgeText animated:YES];
}

- (void)dealloc
{
    [_upView release];
    [_downView release];
    [_emptyDownView release];
    [_titleLabel release];
    [_themeColor release];
    [_downBgView release];
    [_badgeView release];
    [super dealloc];
}


- (void)setTitle:(NSString *)title icon:(UIImage *)icon themeColor:(UIColor *)themeColor subItems:(NSDictionary *)dicSubItems
{
    self.upView.iconView.image = icon;
    self.titleLabel.text = title;
    self.themeColor = themeColor;
    
    if (dicSubItems != nil)
    {
        self.titleLabel.textColor = [UIColor whiteColor];
        self.titleLabel.frame = CGRectMake(self.titleLabel.left, self.height-DOWNVIEWH-TITLEFONTH-TV_MARGIN, self.titleLabel.width, self.titleLabel.height);
        
        [self.upView moveIconUpAnimated:YES];
        
        [self setSubItems:dicSubItems animated:YES];
        [self.emptyDownView removeFromSuperview];
        [self.downBgView addSubview:self.downView];
    }
}

- (void)setSubItems:(NSDictionary *)dicSubItems animated:(BOOL)bAnimated
{
    if (self.downView == nil)
    {
        CTileDownView *downView = [[CTileDownView alloc] initWithFrame:self.downBgView.bounds];
        self.downView = downView;
        self.downView.valueLabel1.textColor = self.themeColor;
        self.downView.valueLabel2.textColor = self.themeColor;
        [downView release];
    }
    
    [self.downView setTitle1:[dicSubItems objectForKey:@"title1"] value1:[dicSubItems objectForKey:@"value1"] title2:[dicSubItems objectForKey:@"title2"] value2:[dicSubItems objectForKey:@"value2"] animated:YES];
//    [self.downView setTitle1:[dicSubItems objectForKey:@"title1"]];
//    [self.downView setTitle2:[dicSubItems objectForKey:@"title2"]];
//    if (![self.downView.valueLabel1.text isEqualToString:[dicSubItems objectForKey:@"value1"]])
//    {
//        [self.downView setValue1:[dicSubItems objectForKey:@"value1"] animated:bAnimated];
//    }
//    if (![self.downView.valueLabel2.text isEqualToString:[dicSubItems objectForKey:@"value2"]])
//    {
//        [self.downView setValue2:[dicSubItems objectForKey:@"value2"] animated:bAnimated];
//    }
}

- (void)updateSubItems:(NSDictionary *)dicSubItems
{
    if (self.downView == nil)
    {
        [self setSubItems:dicSubItems animated:NO];
        [UIView transitionFromView:self.emptyDownView toView:self.downView duration:DURATION options:UIViewAnimationOptionTransitionFlipFromTop | UIViewAnimationOptionAllowAnimatedContent completion:^(BOOL finished) {
            [self.emptyDownView removeFromSuperview];
        }];
        
        [self.upView moveIconUpAnimated:YES];
        
        [UIView animateWithDuration:DURATION/2 animations:^{
            self.titleLabel.frame = CGRectMake(self.titleLabel.left, self.height-DOWNVIEWH-TITLEFONTH/2, self.titleLabel.width, self.titleLabel.height);
            self.titleLabel.alpha = 0.0;
        } completion:^(BOOL finished) {
            self.titleLabel.textColor = [UIColor whiteColor];
            [UIView animateWithDuration:DURATION/2 animations:^{
                self.titleLabel.frame = CGRectMake(self.titleLabel.left, self.height-DOWNVIEWH-TITLEFONTH-TV_MARGIN, self.titleLabel.width, self.titleLabel.height);
                self.titleLabel.alpha = 1.0;
            } completion:^(BOOL finished) {
                
            }];
        }];
    }
    else
    {
        [self setSubItems:dicSubItems animated:YES];
    }
}
//
//- (void)setInfoWithTitle1:(NSString *)title1 value1:(NSString *)value1 title2:(NSString *)title2 value2:(NSString *)value2
//{
//    if (self.downView == nil)
//    {
//        CTileDownView *downView = [[CTileDownView alloc] initWithFrame:self.downBgView.bounds];
//        self.downView = downView;
//        [downView setTitle1:title1];
//        [downView setTitle2:title2];
//        [downView setValue1:value1 animated:NO];
//        [downView setValue2:value2 animated:NO];
//        downView.valueLabel1.textColor = self.themeColor;
//        downView.valueLabel2.textColor = self.themeColor;
//        [downView release];
//        [UIView transitionFromView:self.emptyDownView toView:downView duration:DURATION options:UIViewAnimationOptionTransitionFlipFromTop completion:^(BOOL finished) {
//            [self.emptyDownView removeFromSuperview];
//        }];
//        
//        [self.upView moveIconUp];
//        
//        [UIView animateWithDuration:DURATION/2 animations:^{
//            self.titleLabel.frame = CGRectMake(self.titleLabel.left, self.height-DOWNVIEWH-TITLEFONTH/2, self.titleLabel.width, self.titleLabel.height);
//            self.titleLabel.alpha = 0.0;
//        } completion:^(BOOL finished) {
//            self.titleLabel.textColor = [UIColor whiteColor];
//            [UIView animateWithDuration:DURATION/2 animations:^{
//                self.titleLabel.frame = CGRectMake(self.titleLabel.left, self.height-DOWNVIEWH-TITLEFONTH-TV_MARGIN, self.titleLabel.width, self.titleLabel.height);
//                self.titleLabel.alpha = 1.0;
//            } completion:^(BOOL finished) {
//                
//            }];
//        }];
//    }
//    else
//    {
//        [self.downView setTitle1:title1];
//        [self.downView setTitle2:title2];
//        [self.downView setValue1:value1 animated:YES];
//        [self.downView setValue2:value2 animated:YES];
//    }
//}

@end
